package com.yunfangdata.service.impl;

import com.yunfangdata.domain.Employee;
import com.yunfangdata.domain.RecruitPlan;
import com.yunfangdata.mapper.RecruitPlanDetailMapper;
import com.yunfangdata.mapper.RecruitPlanMapper;
import com.yunfangdata.query.QueryObject;
import com.yunfangdata.query.RecruitQueryObject;
import com.yunfangdata.service.IRecruitPlanService;
import com.yunfangdata.util.PageResult;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class RecruitPlanServiceImpl implements IRecruitPlanService {


    @Autowired
    private RecruitPlanMapper mapper;

    @Autowired
    private RecruitPlanDetailMapper detailMapper;

    public void save(RecruitPlan entity) {
        Employee currentUser = (Employee) SecurityUtils.getSubject().getPrincipals().getPrimaryPrincipal();

        entity.setInputUser(currentUser);
        entity.setInputTime(new Date());

        mapper.insert(entity);
    }

    public void delete(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    public void update(RecruitPlan entity) {
        mapper.updateByPrimaryKey(entity);
    }

    public RecruitPlan get(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    public List<RecruitPlan> list() {
        return mapper.selectAll();
    }

    public PageResult query(QueryObject qo) {
        int total = mapper.queryForCount(qo);

        if (total == 0){
            return new PageResult();
        }
        List<RecruitPlan> rows = mapper.queryForList(qo);
        return new PageResult(total,rows);
    }

    public void changeState(RecruitQueryObject qo) {
        Employee currentUser = (Employee) SecurityUtils.getSubject().getPrincipals().getPrimaryPrincipal();

        Long recruitId = qo.getRecruitId();
        RecruitPlan recruitPlan = mapper.selectByPrimaryKey(recruitId);

        recruitPlan.setApprover(currentUser);
        recruitPlan.setState(qo.getRet());
        recruitPlan.setId(recruitId);
        mapper.updateByPrimaryKey(recruitPlan);
    }

    public void removeRecruit(Long id) {
        mapper.deleteByPrimaryKey(id);

        detailMapper.deleteByRecruitId(id);
    }




    public void firstSave() {
        RecruitPlan recruitPlan = null;
        try {
            RecruitPlan record = new RecruitPlan();

        /*Employee currentUser = (Employee) SecurityUtils.getSubject().getPrincipals().getPrimaryPrincipal();

        record.setInputUser(currentUser);*/

            record.setInputTime(new Date());
            mapper.insert(record);


        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
